package com.lian.pet.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lian.pet.common.basic.enums.DataTypeEnum;
import com.lian.pet.common.cache.redis.service.RedisService;
import com.lian.pet.common.oss.aliyun.config.properties.AliYunOssProperties;
import com.lian.pet.domain.dto.AddPetsDTO;
import com.lian.pet.domain.dto.QueryPetsDTO;
import com.lian.pet.domain.entity.Pets;
import com.lian.pet.domain.vo.PetsVO;
import com.lian.pet.factory.ResponseBeanFactory;
import com.lian.pet.mapper.PetsMapper;
import com.lian.pet.service.PetsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PetsServiceImpl implements PetsService {
    private final PetsMapper petsMapper;
    private final AliYunOssProperties ossProperties;
    private final RedisService redisService;

    @Override
    public void addPets(AddPetsDTO req, String openId) {
        Pets pets = ResponseBeanFactory.getPets(req, ossProperties.getUrlPrefix(), openId);
        petsMapper.insert(pets);
        // 添加数据时 删除Cache数据
        redisService.delKey(DataTypeEnum.PETS.name());
        log.info("执行成功[新增宠物]");
    }

    @Override
    public List<PetsVO> queryPets(QueryPetsDTO req, String openId) {
        IPage<Pets> page = new Page<>(req.getPageNum(), req.getPageSize());
        IPage<Pets> iPage = petsMapper.selectPage(page, Wrappers.<Pets>lambdaQuery()
                .eq(Pets::getOpenId, openId)
                .eq(StringUtils.isNotBlank(req.getOpenId()), Pets::getOpenId, req.getOpenId())
                .orderByDesc(Pets::getUpdateTime));
        List<Pets> pets = iPage.getRecords();
        if (pets.isEmpty()) {
            return Collections.emptyList();
        }
        List<PetsVO> petsVOS = new ArrayList<>();
        pets.forEach(pet -> {
            PetsVO petsVO = PetsVO.fromPets(pet, ossProperties.getUrlPrefix());
            petsVOS.add(petsVO);
        });

        log.info("执行成功[查询宠物列表]");
        return petsVOS;
    }

    @Override
    public PetsVO getPets(Integer id) {
        Pets pets = petsMapper.selectById(id);
        return PetsVO.fromPets(pets, ossProperties.getUrlPrefix());
    }

    @Override
    public void deletePets(Integer id){
        petsMapper.deleteById(id);
    }

    @Override
    public void updatePets(Integer id, AddPetsDTO req) {
        Pets existingPet = petsMapper.selectById(id);
        if (existingPet == null) {
            // 如果找不到对应的宠物信息，则抛出异常或者返回错误信息
            // 这里我选择抛出一个运行时异常示例
            throw new RuntimeException("宠物信息不存在");
        }

        // 更新宠物信息
        existingPet.setName(req.getName());
        existingPet.setBreed(req.getBreed());
        existingPet.setAgeYear(req.getAgeYear());
        existingPet.setAgeMonth(req.getAgeMonth());
        existingPet.setWeight(req.getWeight());
        existingPet.setPhotoUrl(req.getPhotoUrl().replace(ossProperties.getUrlPrefix(), ""));
        existingPet.setUpdateTime(new Date());

        petsMapper.updateById(existingPet);

        // 更新数据时 删除Cache数据
        redisService.delKey(DataTypeEnum.PETS.name());

        log.info("执行成功[修改宠物信息]");
    }

}
